<?php
namespace Home\Controller;
use Think\Controller;
class GoodsController extends Controller {
    public function goods(){
        $goodsinfo = D('Admin/goods')->find(I('goods_id'));
        if($goodsinfo){
            $his = $this->his($goodsinfo);
        }
        $this->assign('mbx',$this->mbx($goodsinfo['cat_id']));
        $this->assign('goodsinfo',$goodsinfo);
        $this->display();
    }
    //历史
    public function his($goodsinfo){
        $goods_name = $goodsinfo['goods_name'];
        $shop_price = $goodsinfo['shop_price'];
        $goods_id = $goodsinfo['goods_id'];
        // $his = array();
        $his = session('?his')?session('his'):array();
        if(count($his) > 3){
            $k = key($his);
            unset($his[$k]);
        }
        $his[$goods_id] = array(
            'goods_name' =>$goods_name,
            'shop_price' =>$shop_price,
        );

        session('his',$his);
    }

    //添加购物车

    public function gwc(){
      $goodsinfo=D('Admin/goods')->find(I('get.goods_id'));
      $tool=\Home\Tool\AddTool::getIns();
      $tool->add($goodsinfo['goods_id'],$goodsinfo['goods_name'],$goodsinfo['shop_price']);
      var_dump(session('buy'));
    }

    public function mbx($cat_id){
        $catModel = D("Admin/Cat");
        $fm = array();
        while($cat_id > 0){
            foreach ($catModel->select() as $k => $v) {
                if($cat_id == $v['cat_id']){
                    $fm[] = $v;
                    $cat_id = $v['parent_id'];
                    break;
                }
            }
        }
        return array_reverse($fm);
    }

}
